import DatabaseUtils from "../utils/db-utils";

class FileDao {

    /**
     * 动态查询表
     * @param where
     * @returns
     */
    static queryTable = async (where: any[]) => {
        const tableName = 't_file';
        const columns = ['id', 'user_id', 'file_id', 'file_name', 'suffix', 'file_size', 'content_type', 'create_time', 'update_time'];
        const sqlObj = DatabaseUtils.getDynamicSelectObj(tableName, columns, where);
        return await DatabaseUtils.execSql(sqlObj.sql, sqlObj.params);
    };

    /**
     * 动态更新表
     * @param columns
     * @param where
     */
    static updateTable = async (columns: any[], where: any[]) => {
        const tableName = 't_file';
        const sqlObj = DatabaseUtils.getDynamicUpdateObj(tableName, columns, where);
        return await DatabaseUtils.execSql(sqlObj.sql, sqlObj.params);
    };

    /**
     * 保存文件
     * @param userId
     * @param fileId
     * @param fileName
     * @param fileSize
     * @param contentType
     * @param suffix
     */
    static saveFile = async (userId: string, fileId: string, fileName: string, fileSize: number, contentType: string, suffix: string) => {
        const sql = `insert into t_file(user_id, file_id, file_name, file_size, content_type, suffix) values (?, ?, ?, ?, ?, ?)`;
        const params = [userId, fileId, fileName, fileSize, contentType, suffix];
        return await DatabaseUtils.execSql(sql, params);
    }
}

export default FileDao;